<?php

namespace app\backend\controller\hrm;

use app\common\controller\Basic;
use app\common\service\hrm\CompanyService;

/**
 * 公司单位
 * @author kaige
 * @update 2023-08-09
 */
class Company extends Basic
{
  // 初始化
  protected function initialize()
  {
    parent::initialize();
    // 当前控制器服务实例化
    $this->service = new CompanyService();
  }

  /**
   * 列表 -- Layui treeTable 特殊树型
   */
  public function index()
  {
    if ($this->request->isGet()) {
      $data = input('param.');
      $result = $this->service->_list($data, false);
      $tree = [];
      if ($result && $result['count'] > 0) {
        $tree = \app\common\util\TreeUtil::list_to_tree($result['data'], 0, 'id', 'pid', 'children');
        \app\common\util\TreeUtil::tree_add_field($tree);
      }
      return json(_layui($result['count'], $tree));
    }
    return _jerr(1000);
  }

  /**
   * 添加
   */
  public function add()
  {
    if ($this->request->isPost()) {
      $data = input('param.');
      $this->_form_filter($data);
      return json($this->service->saveForm($data));
    }
    if ($this->request->isGet()) {
      return _jok();
    }
    return _jerr(1000);
  }

  /**
   * 查看
   */
  public function look()
  {
    if ($this->request->isGet()) {
      $id = input('id/d', 0);
      $data = $this->service->getInfoById($id);
      if (isset($data['logo'])) {
        $data['logo_url'] = _img($data['logo']);
      }
      if (isset($data['bl_img'])) {
        $data['bl_img_url'] = _img($data['bl_img']);
      }
      if (isset($data['bl_sfz_img'])) {
        $data['bl_sfz_img_url'] = _img($data['bl_sfz_img']);
      }
      $data['company_id_name'] = $this->service->getNameById($data['pid']);
      return _jok('ok', $data);
    }
    return _jerr(1000);
  }

  /**
   * 编辑
   */
  public function edit()
  {
    if ($this->request->isPost()) {
      $data = input('param.');
      $this->_form_filter($data);
      //当前是修改，就需要判断是否会陷入死循环
      if ($this->service->checkDie($data['id'], $data['pid']) === false) {
        return _jerr('上一级选错了吧，会陷入死循环的');
      }
      return json($this->service->saveForm($data));
    }
    if ($this->request->isGet()) {
      $id = input('id/d', 0);
      $data = $this->service->getInfoById($id);
      if (isset($data['logo'])) {
        $data['logo_url'] = _img($data['logo']);
      }
      if (isset($data['bl_img'])) {
        $data['bl_img_url'] = _img($data['bl_img']);
      }
      if (isset($data['bl_sfz_img'])) {
        $data['bl_sfz_img_url'] = _img($data['bl_sfz_img']);
      }
      return _jok('ok', $data);
    }
    return _jerr(1000);
  }

  /**
   * 添加编辑-表单数据处理
   * @param array $data
   */
  protected function _form_filter(&$data)
  {
    url_decode($data);
    if ($this->request->isPost()) {
      $data['status'] = checkon('status', $data);
      if (isset($data['id']) && isset($data['pid'])) {
        if ($data['id'] == $data['pid']) {
          $data['pid'] = 0;
        }
      }
    }
  }

  /**
   * 删除
   */
  public function del()
  {
    if ($this->request->isPost()) {
      $id = input('id/d', 0);
      if ($id == env('COMPANY_ID', 11)) {
        return _jerr('基础数据，不允许删除');
      }
      $list = $this->service->getListAll([
        ['pid', '=', $id]
      ]);
      if ($list) {
        return _jerr('删除失败，存在子项');
      } else {
        if ($this->service->delete($id)) {
          return _jok(1025);
        };
      }
      return _jerr(1026);
    }
    return _jerr(1000);
  }

  /**
   * 启/禁用
   */
  public function setdisabled()
  {
    if ($this->request->isPost()) {
      $id = input('id/d', 0);
      $field = input('field', '');
      $value = input('value/d', 0);
      if ($this->service->updateField($id, $field, $value)) {
        return _jok(1031);
      };
      return _jerr(1032);
    }
    return _jerr(1000);
  }

  /**
   * 排序
   */
  public function setsort()
  {
    if ($this->request->isPost()) {
      $id = input('id/d', 0);
      $field = input('field', '');
      $value = input('value/d', 0);
      if ($this->service->updateField($id, $field, $value)) {
        return _jok(1033);
      };
      return _jerr(1032);
    }
    return _jerr(1000);
  }
}
